#!/usr/bin/env node
var fs          = require('fs');
var readline    = require('readline');
var when        = require('when');
var dotEvery    = 10000;

exports.FrequencySampler = ( function() {

    function FrequencySampler(populatedEntries, inputFileName) {
        this.notFound   = {};
        this.mostFrequent = {}
        this.parsed = 0;
        this.input = inputFileName;
        this.entries = populatedEntries;
        this.dependencies = [module.filename];
    }

    FrequencySampler.prototype.do = function(cB) {
        var result = when.defer();
        var self = this;
        var rd = readline.createInterface({
            input: fs.createReadStream(this.input),
            output: process.stdout,
            terminal: false
        });

        rd.on('line', function(line) {
            var words = line.split(' ');
            for (var i = 0; i < words.length; i++) {
                var word = words[i].toLowerCase();
                if (word.length > 1) {
                    self.parsed++;
                    var found = false;
                    if (word in self.entries) {
                        found=true;
                        var entry = self.entries[word];
                        if (!("hits" in entry)) {
                            entry["hits"] = 1;
                        } else {
                            entry["hits"]++;
                        }
                    }
                    if (!(word in self.mostFrequent)) {
                        self.mostFrequent[word] = {
                            hits    : 1,
                            found   : found
                        }
                    } else {
                        self.mostFrequent[word].hits++;
                    }
                }
            }
        });

        rd.on('close', function() {
            var found = 0;
            var mostFrequentArray = [];
            for (var wordKey in self.mostFrequent) {
                mostFrequentArray.push({
                    word    : wordKey,
                    hits    : self.mostFrequent[wordKey].hits,
                    found   : self.mostFrequent[wordKey].found
                });
                found += (self.mostFrequent[wordKey].found ? 1 : 0);
            }

            console.log("Frequency analysis complete. The sample:");
            console.log(" Contained "+self.parsed+" words");
            console.log(" of which "+mostFrequentArray.length+" unique.");
            console.log(" of which "+found+" ("+Math.round(10000*found/mostFrequentArray.length)/100+"%) found in the dictionary.");
            console.log(" Top 20 most popular in the sample:");
            mostFrequentArray.sort(function(a,b) {
                return  b.hits - a.hits;
            });
            var reportMax = Math.min(mostFrequentArray.length, 20);
            for (var i = 0; i < reportMax; i++) {
                console.log("["+mostFrequentArray[i].hits+"]\t"+(mostFrequentArray[i].found?"[    found]":"[not found]")+"\t:"+mostFrequentArray[i].word);
            }
            result.resolve();
        });

        return result.promise;
    }

    return FrequencySampler;
})();
